package boogui.movie;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;

public class MovieDAO implements IMovieDAO {

	MovieSQL mvsql = null;
	IBooguiDAO bdao = null;

	public MovieDAO() {
		mvsql = new MovieSQL();
		bdao = new BooguiDAO();
	}

	@Override
	public boolean insertMovie(MovieVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getInsertMovieSQL());
			pstmt.setString(1, m.getMovtitle());
			pstmt.setString(2, m.getMovgenre1());
			pstmt.setString(3, m.getMovgenre2());
			pstmt.setString(4, m.getMovdirector());
			pstmt.setString(5, m.getMovactor1());
			pstmt.setString(6, m.getMovactor2());
			pstmt.setString(7, m.getMovactor3());
			pstmt.setString(8, m.getMovrating());
			pstmt.setString(9, m.getMovsynop());
			pstmt.setString(10, m.getMovruntime());
			pstmt.setString(11, m.getMovplaydate());
			pstmt.setString(12, m.getMovisplay());
			pstmt.setString(13, m.getMovresv());
			pstmt.executeUpdate();
			result = true;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}

		return result;
	}
	@Override
	public List<MovieVO> selectMovie(MovieVO mv) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getSelectMovieSQL());
			pstmt.setString(1, mv.getMovtitle());
			rs = pstmt.executeQuery();
			while (rs.next()) {		
				
				mv.setMovno(rs.getString("movno"));
				mv.setMovtitle(rs.getString("title"));
				mv.setMovgenre1(rs.getString("genre1"));
				mv.setMovgenre2(rs.getString("genre2"));
				mv.setMovdirector(rs.getString("director"));
				mv.setMovactor1(rs.getString("actor1"));
				mv.setMovactor2(rs.getString("actor2"));
				mv.setMovactor3(rs.getString("actor3"));
				mv.setMovrating(rs.getString("rating"));
				mv.setMovsynop(rs.getString("synop"));
				mv.setMovruntime(rs.getString("runningtime"));
				mv.setMovplaydate(rs.getString("playdate"));
				mv.setMovisplay(rs.getString("isplay"));
				mv.setMovresv(rs.getString("reserverate"));
				result.add(mv);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}

		return result;
	}

	@Override
	public boolean modifyMovie(MovieVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getUpdateMovieSQL());

			pstmt.setString(1, m.getMovtitle());
			pstmt.setString(2, m.getMovgenre1());
			pstmt.setString(3, m.getMovgenre2());
			pstmt.setString(4, m.getMovdirector());
			pstmt.setString(5, m.getMovactor1());
			pstmt.setString(6, m.getMovactor2());
			pstmt.setString(7, m.getMovactor3());
			pstmt.setString(8, m.getMovrating());
			pstmt.setString(9, m.getMovsynop());
			pstmt.setString(10, m.getMovruntime());
			pstmt.setString(11, m.getMovplaydate());
			pstmt.setString(12, m.getMovisplay());
			pstmt.setString(13, m.getMovresv());
			pstmt.executeUpdate();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}

		return result;
	}

	@Override
	public boolean deleteMovie(MovieVO m) {

		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getDeleteMovieSQL());
			pstmt.setString(1, m.getMovno());
			pstmt.executeUpdate();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}

		return result;
	}

}